module hong {
    /** 成语填空的主类 */
	export class cytkgame extends baseGame {
        /** 选择文字面板 */
		private btlist:eui.Group;
        /** 成语文字0 */
        private txt0:eui.Label;
        /** 成语文字1 */
        private txt1:eui.Label;
        /** 成语文字2 */
        private txt2:eui.Label;
        /** 成语文字3 */
        private txt3:eui.Label;
        /** 成语解释  */
        private txtd:eui.Label;

        /** 成语列表 */
		private CYlist:any;
        /** 当前选择的最高册 */
		public sjLevel:number = 0;

        public constructor() {
			super();
			this.RES = "cyxxl";
            this.skinName = "cytkSkin";

            this.btlist.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onBtlist, this);
        }

        /** 游戏显示 */
		public gameShow():void{
            this.CYlist || (this.CYlist = RES.getRes("cy_json"));
            let cylist = [];
            if(this.ext == ""){
                cylist = this.CYlist.list;
            }else{
                this.sjLevel = parseInt(this.ext);
				if(this.sjLevel < 2) this.sjLevel = 2;
                for(let i=1;i<=this.sjLevel;i++){
					if(i in this.CYlist.sj) cylist = cylist.concat(this.CYlist.sj[i]);
				}
            }
            //选择成语
            let cy = cylist[Math.random()*cylist.length|0];
            let pyp = cy[1].indexOf("\n");
            let py = cy[1].substr(0, pyp).trim(" ").split(" ");
            this.txt0.text = cy[0][0] + "\n" + (py[0] || "");
            this.txt1.text = cy[0][1] + "\n" + (py[1] || "");
            this.txt2.text = cy[0][2] + "\n" + (py[2] || "");
            this.txt3.text = cy[0][3] + "\n" + (py[3] || "");
            this.txtd.text = cy[1].substr(pyp);
            //选择一个要填空的字
            let index = Math.random() * 4 | 0;
            this["txt" + index].visible = false;
            //显示可选择的字
            for(let i=0;i<15;i++){
                let rcy = cylist[Math.random()*cylist.length|0][0];
                (this.btlist.getChildAt(i) as eui.Button).label = rcy[Math.random() * 4 | 0];
            }
            //随机一个正确的字
            (this.btlist.getChildAt(Math.random() * 15 | 0) as eui.Button).label = this["txt" + index].text[0];
        }

        /** 点击可选择的文字 */
        private onBtlist(e:egret.TouchEvent):void{
            let bt = e.target as eui.Button;
            if(!bt) return;
            for(let i = 0;i < 4;i++){
                if(this["txt" + i].visible == false){
                    if(bt.label == this["txt" + i].text[0]){
                        this["txt" + i].visible = true;
                        this["txt" + i].alpha = 0;
                        egret.Tween.get(this["txt" + i]).to({alpha:1}, 1000).call(this.gameShow, this);
                        break;
                    }
                }
            }
        }
    }
}